/*
 * Copyright 2009-2010 the original author or authors.
 *
 * Licensed under the Apache license, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.internna.iwebjtracker.spring.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.internna.iwebjtracker.model.TrackerUser;
import org.internna.iwebjtracker.server.ServerManager;
import org.internna.iwebjtracker.services.ActivityReporter;
import org.internna.iwebjtracker.services.TrackerUserManager;
import org.internna.iwebmvc.dao.SecurityDAO;
import org.internna.iwebmvc.model.User;
import org.internna.iwebmvc.security.UserManager;
import org.internna.iwebmvc.spring.mvc.interceptors.ModelDecoratorInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

/**
 * Adds the ServerManager instance to the collection of objects available to the views.
 *
 * @author Jose Noheda
 * @since 1.0
 */
public final class IWebJTrackerModelDecoratorInterceptor extends ModelDecoratorInterceptor {

    public static final String AUTHORITIES = "authorities";
    public static final String TRACKER_USER = "trackerUser";
    public static final String SERVER_MANAGER = "serverManager";
    public static final String ACTIVITY_REPORTER = "activityReporter";

    @Autowired private SecurityDAO dao;
    @Autowired private UserManager userManager;
    @Autowired private ServerManager serverManager;
    @Autowired private ActivityReporter activityReporter;
    @Autowired private TrackerUserManager trackerUserManager;

    /**
    * Retrieves the model and if exists adds the configured serverManager.
    */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
        if (modelAndView != null) {
            ModelMap model = modelAndView.getModelMap();
            if (model != null) {
                model.addAttribute(SERVER_MANAGER, serverManager);
                model.addAttribute(ACTIVITY_REPORTER, activityReporter);
                if (logger.isDebugEnabled()) {
                    logger.debug("Populating view with [" + SERVER_MANAGER + "=" + serverManager + "]");
                    logger.debug("Populating view with [" + ACTIVITY_REPORTER + "=" + activityReporter + "]");
                }
                User user = userManager.getActiveUser();
                if ((user != null) && (!user.isAnonymous())) {
                    TrackerUser trackerUser = trackerUserManager.getTrackerUser(user.getUsername());
                    model.addAttribute(TRACKER_USER, trackerUser);
                    model.addAttribute(AUTHORITIES, dao.findAuthorities());
                    if (logger.isDebugEnabled()) {
                        logger.debug("Populating view with [" + TRACKER_USER + "=" + trackerUser + "]");
                    }
                }
            }
        }
    }

}
